﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PerceptiveMCAPI.Export.Types;
//
namespace PerceptiveMCAPI.Export
{
	internal static class ValidateInput
	{
		#region ================================================================== IsOK & Validate Base

		// check for errors
		internal static bool IsOK( List<Api_ValidatorMessage> messages )
		{
			foreach ( Api_ValidatorMessage message in messages )
			{
				if ( message.MessageLevel >= EnumValues.MessageLevel.Error )
				{ return false; }
			}
			return true;
		}
		//
		internal static Api_ValidatorMessage FormatMsg( EnumValues.MessageLevel msg_level, string msg_property, string msg_text )
		{
			Api_ValidatorMessage val = new Api_ValidatorMessage
			{
				MessageLevel = msg_level,
				PropertyName = msg_property,
				MessageText = msg_text
			};
			return val;
		}

		#endregion

		#region --------------------------------- Generic existence, type checks

		// check for empty string
		internal static Api_ValidatorMessage string_value_required( string value, string parmName )
		{
			if ( string.IsNullOrEmpty( value ) )
			{
				return FormatMsg( EnumValues.MessageLevel.Error, parmName,
					string.Format( "Required parameter '{0}' was not specified.", parmName ) );
			}
			return null;
		}
		// check for positive integer
		internal static Api_ValidatorMessage int_value_required( int value, string parmName )
		{
			if ( value < 1 )
			{
				return FormatMsg( EnumValues.MessageLevel.Error, parmName,
					string.Format( "Integer value '{0}' must be greater than 0.", parmName ) );
			}
			return null;
		}
		// check of type
		internal static Api_ValidatorMessage parameter_type_valid( string name, string type, object parm )
		{
			string parm_type = parm.GetType().Name;
			if ( parm_type.ToLower() != type.ToLower() )
			{
				return FormatMsg( EnumValues.MessageLevel.Error, name,
					string.Format( "Parameter '{0}' type is {1}. Type of {2} was expected.", name, parm_type, type ) );
			}
			return null;
		}

		// check collection has entries
		internal static Api_ValidatorMessage collection_has_entries( ICollection value, string parmName )
		{
			if ( value == null && value.Count == 0 )
			{
				return FormatMsg( EnumValues.MessageLevel.Error, parmName,
					string.Format( "Collection '{0}' is null or empty.", parmName ) );
			}
			return null;
		}

		internal static Api_ValidatorMessage since_valid( DateTime since )
		{
			if ( since > DateTime.UtcNow )
			{
				return FormatMsg( EnumValues.MessageLevel.Warning, "since", "'since' date/time cannot be in the future. All times are UTC." );
			}
			return null;
		}


		#endregion
	}
}

